﻿#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace TLib.Xna
{
    public class GameObject
    {
        protected Model model; //Model of the object
        protected Vector3 pos = new Vector3(0,0,0); //Position of the object
        protected float pitch = 0f; //Pitch orientation
        protected float yaw = 0f; //Yaw orientation
        protected float roll = 0f; //Roll orientation
        protected float scale = 1f; //Scale of model
        public Matrix World { get; set; }

        protected bool snapHeight = true;
        protected bool autoAdjustAngle = true;

        public GameObject(Model _model) { model = _model; }
        public GameObject(Model _model, Vector3 _pos, float _pitch, float _yaw, float _roll) 
        { 
            model = _model;
            pos = _pos;
            pitch = MathHelper.ToRadians(_pitch);
            yaw = MathHelper.ToRadians(_yaw);
            roll = MathHelper.ToRadians(_roll);

            //foreach (ModelMesh mesh in model.Meshes)
            //{
            //    foreach (BasicEffect effect in mesh.Effects)
            //    {
            //        effect.LightingEnabled = true;
            //        effect.DirectionalLight0.Enabled = true;
            //        effect.AmbientLightColor = new Vector3(0.3f, 0.3f, 0.3f);
            //        effect.DirectionalLight0.DiffuseColor = new Vector3(0.3f, 0.3f, 0.2f);
            //        effect.DirectionalLight0.Direction = Vector3.Normalize(new Vector3(0f, -1f, 0f));

            //        effect.DirectionalLight1.Enabled = true;
            //        effect.DirectionalLight1.DiffuseColor = new Vector3(0.2f, 0.2f, 0.0f);
            //        effect.DirectionalLight1.Direction = Vector3.Normalize(new Vector3(-0.5f, -0.5f, -0.5f));

            //        effect.DirectionalLight2.Enabled = true;
            //        effect.DirectionalLight2.DiffuseColor = new Vector3(0.2f, 0.2f, 0.0f);
            //        effect.DirectionalLight2.Direction = Vector3.Normalize(new Vector3(0.5f, -0.5f, -0.5f));
            //    }
            //}

            ResetWorld();
        }
        
        public Vector3 Position
        {
            get { return pos; }
            set
            {
                pos = value;
                ResetWorld();
            }
        }

        public float PositionX
        {
            get { return pos.X; }
            set 
            {
                pos.X = value;
                ResetWorld();
            }
        }

        public float PositionY
        {
            get { return pos.Y; }
            set 
            {
                pos.Y = value;
                ResetWorld();
            }
        }

        public float PositionZ
        {
            get { return pos.Z; }
            set 
            {
                pos.Z = value;
                ResetWorld();
            }
        }

        public Model Model
        {
            get { return model; }
            set 
            { 
                model = value;
                ResetWorld();
            }
        }

        public float Pitch
        {
            get { return pitch; }
            set 
            {
                pitch = value;
                ResetWorld();
            }
        }

        public float Yaw
        {
            get { return yaw; }
            set 
            {
                yaw = value;
                ResetWorld();
            }
        }

        public float Roll
        {
            get { return roll; }
            set 
            { 
                roll = value;
                ResetWorld();
            }
        }

        public float Scale 
        {
            get { return scale; }
            set
            {
                scale = value;
                ResetWorld();
            }
        }

        public bool SnapHeight
        {
            get { return snapHeight; }
            set { snapHeight = value; }
        }

        public bool AutoAdjustAngle
        {
            get { return autoAdjustAngle; }
            set { autoAdjustAngle = value; }
        }

        protected void ResetWorld()
        {
            World = Matrix.Identity * Matrix.CreateFromYawPitchRoll(yaw, pitch, roll) * Matrix.CreateScale(scale) * Matrix.CreateTranslation(pos);
        }

        public virtual void Update(float delta)
        {
        }
    }
}
